﻿using CIS.Web.Common;
using CIS.Web.Models;
using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("Home")]
	public class HomeController : BaseDbController
	{
		#region Logout
		[AllowAnonymous]
		public async Task<ActionResult> MainIndex()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "MainIndex");

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.NewsListName == ConstValues.Slider)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on newListInfo.ID equals newInfo.NewsListId
				  join conferenceInfo in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.ConferenceName == ConstValues.DefaultConference)
					on newListInfo.ConferenceId equals conferenceInfo.ID
				  orderby newInfo.Sort ascending, newInfo.UpdateDate descending
				  select new
				  {
					  ID = newInfo.ID,
					  Sort = newInfo.Sort,
					  Link = newInfo.Link
				  };

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new SliderModel
			{
				id = x.ID,
				link = x.Link
			});

			var model = new HomeModel();

			model.SliderList = result.ToList();

			//近期召开会议
			var meetinghome = DbContext.ConferenceMaster.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.IsDisplay && x.IsInHome);
					

			if (Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En)
			{
				meetinghome = meetinghome.Where(x => x.IsInternational);
			}
			else
			{
				meetinghome = meetinghome.Where(x => !x.IsInternational);
			}

			meetinghome = meetinghome.OrderByDescending(x => x.ConferenceStartDate).ThenBy(x => x.Sort);

			var inhomeResult = await meetinghome.ToListAsync();

			var hResult = inhomeResult.Select(x => new MeetingInHomeModel { 
				id = x.ID,
				conferenceName = x.ConferenceName,
				homepageUrl = x.HomePageUrl,
				allowContribute = x.ContributeStartDate.Value <= DateTime.Now && x.ContributeEndDate.Value >= DateTime.Now ? true : false,
				allowRegister = x.RegisterStartDate.Value <= DateTime.Now && x.RegisterEndDate.Value >= DateTime.Now ? true : false,
				conferenceStartDate = x.ConferenceStartDate.Value.ToString("yyyy-MM-dd"),
				conferenceEndDate = x.ConferenceEndDate.Value.ToString("yyyy-MM-dd"),
				operateLocation = x.OperateLocation,
				conferenceNickName = x.ConferenceNickName,
				hasContribute = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceContributeInfo),
				hasRegister = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceAttendenceInfo)
			});

			model.MeetingInHomeList = hResult.ToList();

			//推荐会议
			var meetingRecommend = DbContext.ConferenceMaster.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.IsDisplay && x.IsRecommend);

			if (Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En)
			{
				meetingRecommend = meetingRecommend.Where(x => x.IsInternational);
			}
			else
			{
				meetingRecommend = meetingRecommend.Where(x => !x.IsInternational);
			}

			meetingRecommend = meetingRecommend.OrderByDescending(x => x.ConferenceStartDate).ThenBy(x => x.Sort);

			var recommendResult = await meetingRecommend.ToListAsync();

			var rResult = recommendResult.Select(x => new MeetingRecommendModel
			{
				id = x.ID,
				conferenceName = x.ConferenceName,
				homepageUrl = x.HomePageUrl,
				allowContribute = x.ContributeStartDate.Value <= DateTime.Now && x.ContributeEndDate.Value >= DateTime.Now ? true : false,
				allowRegister = x.RegisterStartDate.Value <= DateTime.Now && x.RegisterEndDate.Value >= DateTime.Now ? true : false,
				conferenceStartDate = x.ConferenceStartDate.Value.ToString("yyyy-MM-dd"),
				conferenceEndDate = x.ConferenceEndDate.Value.ToString("yyyy-MM-dd"),
				operateLocation = x.OperateLocation,
				conferenceNickName = x.ConferenceNickName,
				hasContribute = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceContributeInfo),
				hasRegister = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceAttendenceInfo)
			});

			model.MeetingRecommendList = rResult.ToList();

			return View(model);
		}

		[AllowAnonymous]
		public async Task<ActionResult> AboutUs()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "AboutUs");

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.NewsListName == ConstValues.AboutUs)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on newListInfo.ID equals newInfo.NewsListId
				  join conferenceInfo in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.ConferenceName == ConstValues.DefaultConference)
					on newListInfo.ConferenceId equals conferenceInfo.ID
				  select new
				  {
					  ID = newInfo.ID,
					  MainText = newInfo.MainText,
					  Sort = newInfo.Sort
				  };

			var sqlResult = await sql.FirstOrDefaultAsync();

			var model = new HomeModel();

			if (sqlResult != null)
			{
				model.Id = sqlResult.ID;
				model.MainText = sqlResult.MainText == null ? string.Empty : sqlResult.MainText;
			}

			return View(model);
		}

		[AllowAnonymous]
		public async Task<ActionResult> Help()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Help");

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.NewsListName == ConstValues.Help)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on newListInfo.ID equals newInfo.NewsListId
				  join conferenceInfo in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.ConferenceName == ConstValues.DefaultConference)
					on newListInfo.ConferenceId equals conferenceInfo.ID
				  select new
				  {
					  ID = newInfo.ID,
					  MainText = newInfo.MainText,
					  Sort = newInfo.Sort
				  };

			var sqlResult = await sql.FirstOrDefaultAsync();

			var model = new HomeModel();

			if (sqlResult != null)
			{
				model.Id = sqlResult.ID;
				model.MainText = sqlResult.MainText == null ? string.Empty : sqlResult.MainText;
			}

			return View(model);
		}
		#endregion

		#region Login
		public async Task<ActionResult> Index()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index");

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.NewsListName == ConstValues.Slider)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on newListInfo.ID equals newInfo.NewsListId
				  join conferenceInfo in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.ConferenceName == ConstValues.DefaultConference)
					on newListInfo.ConferenceId equals conferenceInfo.ID
				  orderby newInfo.Sort ascending, newInfo.UpdateDate descending
				  select new
				  {
					  ID = newInfo.ID,
					  Sort = newInfo.Sort,
					  Link = newInfo.Link
				  };

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new SliderModel
			{
				id = x.ID,
				link = x.Link
			});

			var model = new HomeModel();

			model.SliderList = result.ToList();

			//近期召开会议
			var meetinghome = DbContext.ConferenceMaster.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.IsDisplay && x.IsInHome);

			if (Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En)
			{
				meetinghome = meetinghome.Where(x => x.IsInternational);
			}
			else
			{
				meetinghome = meetinghome.Where(x => !x.IsInternational);
			}

			meetinghome = meetinghome.OrderByDescending(x => x.ConferenceStartDate).ThenBy(x => x.Sort);

			var inhomeResult = await meetinghome.ToListAsync();

			var hResult = inhomeResult.Select(x => new MeetingInHomeModel
			{
				id = x.ID,
				conferenceName = x.ConferenceName,
				homepageUrl = x.HomePageUrl,
				allowContribute = x.ContributeStartDate.Value <= DateTime.Now && x.ContributeEndDate.Value >= DateTime.Now ? true : false,
				allowRegister = x.RegisterStartDate.Value <= DateTime.Now && x.RegisterEndDate.Value >= DateTime.Now ? true : false,
				conferenceStartDate = x.ConferenceStartDate.Value.ToString("yyyy-MM-dd"),
				conferenceEndDate = x.ConferenceEndDate.Value.ToString("yyyy-MM-dd"),
				operateLocation = x.OperateLocation,
				conferenceNickName = x.ConferenceNickName,
				hasContribute = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceContributeInfo),
				hasRegister = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceAttendenceInfo)
			});

			model.MeetingInHomeList = hResult.ToList();

			//推荐会议
			var meetingRecommend = DbContext.ConferenceMaster.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.IsDisplay && x.IsRecommend);

			if (Request.Cookies["_culture"] != null && Request.Cookies["_culture"].Value == ConstValues.Language.En)
			{
				meetingRecommend = meetingRecommend.Where(x => x.IsInternational);
			}
			else
			{
				meetingRecommend = meetingRecommend.Where(x => !x.IsInternational);
			}

			meetingRecommend = meetingRecommend.OrderByDescending(x => x.ConferenceStartDate).ThenBy(x => x.Sort);

			var recommendResult = await meetingRecommend.ToListAsync();

			var rResult = recommendResult.Select(x => new MeetingRecommendModel
			{
				id = x.ID,
				conferenceName = x.ConferenceName,
				homepageUrl = x.HomePageUrl,
				allowContribute = x.ContributeStartDate.Value <= DateTime.Now && x.ContributeEndDate.Value >= DateTime.Now ? true : false,
				allowRegister = x.RegisterStartDate.Value <= DateTime.Now && x.RegisterEndDate.Value >= DateTime.Now ? true : false,
				conferenceStartDate = x.ConferenceStartDate.Value.ToString("yyyy-MM-dd"),
				conferenceEndDate = x.ConferenceEndDate.Value.ToString("yyyy-MM-dd"),
				operateLocation = x.OperateLocation,
				conferenceNickName = x.ConferenceNickName,
				hasContribute = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceContributeInfo),
				hasRegister = DbContext.ConferenceFunctionMaster.AsNoTracking()
						.Any(e => e.ConferenceId == x.ID &&
							e.Status == ConstValues.Status.正常 &&
							e.FunctionId == ConstValues.FunctionId.ConferenceAttendenceInfo)
			});

			model.MeetingRecommendList = rResult.ToList();

			var hasLogin = Session["HasLogin"]?.ToString();
			//model.HasLogin ="1";
			model.HasLogin = !string.IsNullOrEmpty(hasLogin) && hasLogin == "1" ? "1" : "0";
			Session["HasLogin"] = "0";

			return View(model);
		}

		public async Task<ActionResult> AboutUsLogin()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "AboutUsLogin");

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.NewsListName == ConstValues.AboutUs)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on newListInfo.ID equals newInfo.NewsListId
				  join conferenceInfo in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.ConferenceName == ConstValues.DefaultConference)
					on newListInfo.ConferenceId equals conferenceInfo.ID
				  select new
				  {
					  ID = newInfo.ID,
					  MainText = newInfo.MainText,
					  Sort = newInfo.Sort
				  };

			var sqlResult = await sql.FirstOrDefaultAsync();

			var model = new HomeModel();

			if (sqlResult != null)
			{
				model.Id = sqlResult.ID;
				model.MainText = sqlResult.MainText == null ? string.Empty : sqlResult.MainText;
			}

			return View(model);
		}

		public async Task<ActionResult> HelpLogin()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "HelpLogin");

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 && x.NewsListName == ConstValues.Help)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
					on newListInfo.ID equals newInfo.NewsListId
				  join conferenceInfo in DbContext.ConferenceMaster.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常 && x.ConferenceName == ConstValues.DefaultConference)
					on newListInfo.ConferenceId equals conferenceInfo.ID
				  select new
				  {
					  ID = newInfo.ID,
					  MainText = newInfo.MainText,
					  Sort = newInfo.Sort
				  };

			var sqlResult = await sql.FirstOrDefaultAsync();

			var model = new HomeModel();

			if (sqlResult != null)
			{
				model.Id = sqlResult.ID;
				model.MainText = sqlResult.MainText == null ? string.Empty : sqlResult.MainText;
			}

			return View(model);
		}
		#endregion
	}
}